package bigdata.monitor.binlog.utils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;

public class JsonUtils {

    public static String getPrimaryKey(String json, String primaryField) {
        if (primaryField == null || primaryField.trim().isEmpty()) {
            primaryField = "id"; // 默认
        }
        JSONObject obj = JSON.parseObject(json);
        JSONObject after = obj.getJSONObject("after");

        if (after != null && after.containsKey(primaryField)) {
            Object value = after.get(primaryField);
            // Fastjson2 中，getString 会自动 toString，但 null 会返回 null
            return value != null ? value.toString() : "unknown";
        }

        return "unknown";
    }
}
